package com.iafme.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.iafme.dto.req.FileReqDTO;
import com.iafme.properties.EmailProperties;
import com.iafme.utils.ConstantsBase;
import com.systemframework.constants.Constants;
import com.systemframework.exception.ResultException;
import com.systemframework.redis.RedisCache;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author summer
 * @version 1.0
 * @date 2025/3/3 11:51
 */
@Configuration
@AllArgsConstructor
@Slf4j
public class EmailConfig {

    private final EmailProperties emailProperties;

    private final RedisCache redisCache;

    private  final JavaMailSender mailSender;



    public void sendEmailCode(String to){
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(to);
            message.setFrom(new InternetAddress(MimeUtility.encodeText(emailProperties.getFromAlias())+"<"+emailProperties.getUser()+">").toString());
            message.setSubject(emailProperties.getSubject());
            String context = emailProperties.getContext();
            String code = RandomUtil.randomNumbers(emailProperties.getRandomLength());
            String text=StrUtil.format(context,code,emailProperties.getEffectiveTime());
            redisCache.set(ConstantsBase.EMAIL_CODE +to,code,emailProperties.getEffectiveTime(), TimeUnit.MINUTES);
            message.setText(text);
            mailSender.send(message);
        }catch (Exception e){
            log.error(e.getMessage());
            throw new ResultException(e.getLocalizedMessage());
        }
    }

    /**
     * 发送附件、html
     * @param to
     * @param context
     * @param isHtml
     * @param fileList
     * @param subject
     */
    public void sendFile(String[] to, String context, boolean isHtml, List<FileReqDTO> fileList, String subject) throws Exception{
            boolean isAnnex=false;
            if (fileList != null && fileList.size() > 0) {
                isAnnex = true;
            }
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,isAnnex);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSentDate(new Date());
            mimeMessageHelper.setText(context,isHtml);
            mimeMessageHelper.setSubject(subject);
            if(CollUtil.isNotEmpty(fileList) && fileList.size() > 0 ){
                for(FileReqDTO fileReqDTO : fileList){
                    mimeMessageHelper.addAttachment(fileReqDTO.getName(),fileReqDTO.getFile());
                }
            }
            mimeMessageHelper.setFrom(new InternetAddress(emailProperties.getUser(),emailProperties.getFromAlias(), Constants.CODE_UTF_FORMAT));
            mailSender.send(mimeMessage);




    }

}
